<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>后台管理</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.css">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="js/jquery-3.4.1.min.js"></script>
    <!-- 加载jsRender-->
    <script src="js/jsrender.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/jsrender.min.js" type="text/javascript" charset="utf-8"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
    <!-- Bootstrap-table -->
    <script src="js/bootstrap-table.min.js"></script>
    <script src="js/bootstrap-table-zh-CN.min.js"></script>
    <style>
        body {
            background-color: #eee;
        }

        .row {
            padding: 20px;
            margin-bottom: 10px;
        }

        .panel-default>.panel-heading, .panel-warning>.panel-heading,
        .panel-danger>.panel-heading, .panel-info>.panel-heading {
            background-color: white;
            font-weight: bold;
        }

        .row>.panel {
            background-color: white;
            box-shadow: 1px 1px 3px #0f0f0f, -1px -1px 3px #0f0f0f;
        }

        .row {
            background-color: rgba(255, 255, 255, .5);
        }

        .panel-default>.count-top {
            font-size: 14px;
            color: rgba(77, 77, 77, .5);
        }

        .panel-default>.count {
            font-size: 30px;
            margin-left: 20px;
            font-weight: bolder;
            text-align: center;
        }
        .input-group-addon {
            width: 80px;
            font-weight: bold;
            padding: 5px;
        }
        .input-group input {
            padding-left: 10px;
        }
        .input-group .form-check{
            margin-left: 10px;
        }



    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">
                    <img alt="Brand" src="./images/M.svg" width="25" height="25">
                </a>
            </div>
            <div class="collapse navbar-collapse"  id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="./index.html">首页</a></li>
                    <li class="active"><a href="#">用户管理</a></li>
                    <li><a href="#">产品管理</a></li>
                    <li><a href="#">留言管理</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
                            <span id="show-uname" class="text-info" style="margin-left: 5px;">Admin</span>
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li>
                                <a href="">
                                    <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>
                                    <span class="text-info" style="margin-left: 5px;">修改密码</span>
                                </a>
                            </li>
                            <li>
                                <a href="#">
                                    <span class="glyphicon glyphicon-log-out" aria-hidden="true"></span>
                                    <span class="text-info" style="margin-left: 5px;">管理员退出</span>
                                </a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col-md-12 panel panel-info well">
                <div class="panel-heading">
                    <span>用户管理</span>
                    <span>
                        <button id="addUser" class="btn btn-danger btn-sm">添加用户</button>
                    </span>
                </div>
                <table id="table" class="table"></table>
            </div>
        </div>
    </div>
    <div id="body"></div>
    <!-- Modal -->
    <div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <form id="update-form">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel">修改用户数据</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="modal-body">

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button id="btn-submit" type="button" class="btn btn-primary">提交修改</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <!-- Modal2   -->
    <div class="modal fade" id="staticBackdrop1" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <form id="add-form">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel1">添加新用户</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="modal-body1">
                        <ul class="list-group">
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">用户id</span>
                                    <input type="text" aria-readonly="true" readonly class="form-control-plaintext" id="staticEmail" value="由系统自动生成,无法自定义">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">用户名</span>
                                    <input type="text" name="username" class="form-control" id="username" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">密码</span>
                                    <input type="password" name="password" class="form-control" id="password" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">确认密码</span>
                                    <input type="password" class="form-control" id="repassword" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">学号</span>
                                    <input type="text" name="sno" class="form-control" id="sno" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">电话</span>
                                    <input type="text" name="phone" class="form-control" id="phone" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">班级</span>
                                    <input type="text" name="uclass" class="form-control" id="uclass" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">真实姓名</span>
                                    <input type="text" name="realname" class="form-control" id="realname" value="">
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">性别</span>
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="radio" checked="checked" name="sex" id="inlineRadio1" value="1">
                                        <label class="form-check-label" for="inlineRadio1">男</label>
                                        <input class="form-check-input" type="radio" name="sex" id="inlineRadio2" value="0">
                                        <label class="form-check-label" for="inlineRadio2">女</label>
                                    </div>
                                </div>
                            </li>
                            <li class="list-group-item">
                                <div class="input-group">
                                    <span class="input-group-addon">宿舍号</span>
                                    <input type="text" name="dormitoty" class="form-control" id="dormitory" value="">
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button id="add-submit" type="button" class="btn btn-primary">确定添加</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <!-- template-->
    <script id="modalbody" type="text/x-jsrender">
            <ul class="list-group">
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">Uid</span>
                        <input type="text" name="uid" readonly class="form-control-plaintext" id="staticEmail" value="{{:uid}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">用户名</span>
                        <input type="text" name="username" class="form-control" id="username" value="{{:username}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">密码</span>
                        <input type="password" name="password" class="form-control" id="password" value="">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">学号</span>
                        <input type="text" name="sno" class="form-control" id="sno" value="{{:sno}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">电话</span>
                        <input type="text" name="phone" class="form-control" id="phone" value="{{:phone}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">班级</span>
                        <input type="text" name="uclass" class="form-control" id="uclass" value="{{:uclass}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">真实姓名</span>
                        <input type="text" name="realname" class="form-control" id="realname" value="{{:realname}}">
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">性别</span>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" {{if sex==1}}checked="checked"{{/if}} name="sex" id="inlineRadio1" value="1">
                            <label class="form-check-label" for="inlineRadio1">男</label>
                            <input class="form-check-input" type="radio" {{if sex==0}}checked="checked"{{/if}} name="sex" id="inlineRadio2" value="0">
                            <label class="form-check-label" for="inlineRadio2">女</label>
                        </div>
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="input-group">
                        <span class="input-group-addon">宿舍号</span>
                        <input type="text" name="dormitoty" class="form-control" id="dormitory" value="{{:dormitory}}">
                    </div>
                </li>
            </ul>
    </script>
    <script id="modalbody2" type="text/x-jsrender">
        <ul class="list-group">
            <li class="list-group-item">
                <span class="badge">{{:uid}}</span>
                用户id
            </li>
            <li class="list-group-item">
                <span class="badge">{{:username}}</span>
                用户名
            </li>
            <li class="list-group-item">
                <span class="badge">{{:password}}</span>
                密码
            </li>
            <li class="list-group-item">
                <span class="badge">{{:sno}}</span>
                学号
            </li>
            <li class="list-group-item">
                <span class="badge">{{:phone}}</span>
                电话
            </li>
            <li class="list-group-item">
                <span class="badge">{{:uclass}}</span>
                班级
            </li>
            <li class="list-group-item">
                <span class="badge">{{:realname}}</span>
                真实姓名
            </li>
            <li class="list-group-item">
                <span class="badge">
                    {{if sex==1}}
                        男
                    {{/if}}
                    {{if sex==0}}
                        女
                    {{/if}}
                </span>
                性别
            </li>
            <li class="list-group-item">
                <span class="badge">{{:dormitory}}</span>
                宿舍号
            </li>
        </ul>
    </script>
    <script>
        $("#addUser").click(function () {
            $("#staticBackdrop1").modal("show");
        });
        $("#add-submit").click(function () {
            $.ajax({
                type: "POST",   //提交的方法
                url:"http://localhost:8080/ycumall/api/admin.do?method=register", //提交的地址
                data:$("#add-form").serialize(),// 序列化表单值
                async: false,
                dataType: "json",
                error: function(XMLHttpRequest, textStatus, errorThrown) {  //失败的话
                    alert("连接错误!");
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                },
                success: function(data) {//成功
                    if(data.status === 200) {
                        alert("添加成功!")
                    } else {
                        alert("添加失败!");
                    }
                }
            });
            $("#staticBackdrop1").modal('hide');
            $("#table").bootstrapTable("refresh");
        });
        $.get("http://localhost:8080/ycumall/api/admin.do?method=getUserInfo", function (data) {
            $("#show-uname").html(data.data.username);
            console.log(data.data.username);
        });
        $("#btn-submit").click(function () {
            $.ajax({
                type: "POST",   //提交的方法
                url:"http://localhost:8080/ycumall/api/admin.do?method=updateUser", //提交的地址
                data:$('#update-form').serialize(),// 序列化表单值
                async: false,
                dataType: "json",
                error: function(XMLHttpRequest, textStatus, errorThrown) {  //失败的话
                    alert("连接错误!");
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                },
                success: function(data) {  //成功
                    if(data.status === 200) {
                        alert("修改成功!")
                    } else {
                        alert("修改失败!");
                    }
                }
            });
            $("#staticBackdrop").modal('hide');
        });
        function addFunctionAlty(value, row, index) {
            return [
                '<button id="delete" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-remove-circle" style="color: red;"></span></button>',
                '<button id="update" type="button" class="btn btn-primary" style="margin-left: 5px; margin-right: 5px;" data-toggle="modal" data-target="#exampleModalCenter"><span class="glyphicon glyphicon-pencil" style="color:#5bc0de;"></span></button>',
                '<button id="view" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-cloud-download" style="color: #ec971f;"></span></button>'
            ].join('');
        }
        window.operateEvents = {
            'click #delete': function (e, value, row, index) {
                var r = confirm("确定删除该用户?");
                if (r === true) {
                    $("#table").bootstrapTable('removeByUniqueId', row.uid);
                    $.post("http://localhost:8080/ycumall/api/admin.do?method=deleteUser", "uid="+row.uid, function (data) {
                        if (data.status === 200) {
                            alert("删除成功!");
                        }
                    });
                }
            }, 'click #update': function (e, value, row, index) {
                var template = $.templates("#modalbody");
                var htmlOutput = template.render(row);
                $("#modal-body").html(htmlOutput);
                $("#staticBackdrop").modal('show');
            }, 'click #view': function (e, value, row, index) {
                var template = $.templates("#modalbody2");
                var htmlOutput = template.render(row);
                $("#modal-body").html(htmlOutput);
                $("#staticBackdrop").modal('show');
            }
        };
        $('#table').bootstrapTable({
            url: 'http://localhost:8080/ycumall/api/admin.do?method=getUser',
            pagination: true,
            pageSize: 6,
            striped: true,
            sortName: 'uid',
            uniqueId: 'uid',
            sortOrder: 'desc',
            columns: [{
                field: 'uid',
                title: '用户ID'
            }, {
                field: 'username',
                title: '用户名'
            }, {
                field: 'password',
                title: '用户密码'
            }, {
                field: 'sno',
                title: '学号'
            }, {
                field: 'sex',
                title: '性别',
                formatter: function (value) {
                    if (value === 1) {return '男';}
                    else return '女';
                }
            }, {
                field: 'modifytime',
                title: '注册时间',
                formatter: function (value) {
                    return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                }
            }, {
                field: 'operate',
                title: '操作',
                events: operateEvents,//给按钮注册事件
                formatter: addFunctionAlty//表格中增加按钮
            }]
        });
        Date.prototype.format = function(fmt) {
            // 对Date的扩展，将 Date 转化为指定格式的String
            // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
            // 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
            // 例子：
            // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
            // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
            var o = {
                "M+" : this.getMonth()+1,                 //月份
                "d+" : this.getDate(),                    //日
                "h+" : this.getHours(),                   //小时
                "m+" : this.getMinutes(),                 //分
                "s+" : this.getSeconds(),                 //秒
                "q+" : Math.floor((this.getMonth()+3)/3), //季度
                "S"  : this.getMilliseconds()             //毫秒
            };
            if(/(y+)/.test(fmt)) {
                fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
            }
            for(var k in o) {
                if(new RegExp("("+ k +")").test(fmt)){
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
                }
            }
            return fmt;
        };
    </script>
</body>
</html>